package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Fun;
import com.darkplanet.management.domain.PRMFAll;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class FunDao implements IFunDao {
    QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDs());

    @Override
    public Fun getUserById(Integer fun_id) {
        Fun getUserById = null;
        String sql = "select * from fun where fun_id = ?";
        try {
            getUserById = queryRunner.query(sql,new BeanHandler<Fun>(Fun.class),fun_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return getUserById;
    }

    @Override
    public void addFunUser(Integer model_id,String fun_name, String priority, String simple_desc, String complex_desc, String remark) {
        String sql = "insert into fun(model_id,fun_name,priority,simple_desc,complex_desc,remark) values(?,?,?,?,?,?)";
        try {
            queryRunner.update(sql,model_id,fun_name,priority,simple_desc,complex_desc,remark);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteFunUser(Integer fun_id) {
        String sql = "delete from fun where fun_id = ?";
        try {
            queryRunner.update(sql,fun_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateFunUser(Integer model_id,String fun_name,String priority,String simple_desc,String complex_desc,String remark,Integer fun_id) {
        String sql = "update fun set model_id = ?,fun_name = ?,priority = ?,simple_desc = ?,complex_desc = ?,remark = ? where fun_id = ?";
        try {
            queryRunner.update(sql,model_id,fun_name,priority,simple_desc,complex_desc,remark,fun_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List getFunUser() {
        List getFunUser = null;
        String sql = "select * from fun";
        try {
            getFunUser = queryRunner.query(sql,new BeanListHandler<Fun>(Fun.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return getFunUser;
    }

    @Override
    public List<PRMFAll> getFour() {
        String sql = "SELECT * FROM requirement r left join project p on r.project_id = p.project_id\n" +
                "left join model m on r.requirement_id = m.requirement_id\n" +
                "left join fun f on m.model_id = f.model_id";
        List<PRMFAll> prmfAlls = null;
        try {
            prmfAlls = queryRunner.query(sql, new BeanListHandler<PRMFAll>(PRMFAll.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return prmfAlls;
    }


}
